#pragma once

#include<iostream>

using namespace std;



template<class K>
struct BSTreeNode
{
public:
	BSTreeNode(const K& key) :
		key(key),
		left(nullptr),
		right(nullptr)
	{}
	BSTreeNode<K>* left;
	BSTreeNode<K>* right;
	K key;
};

template<class K>
class BSTree
{
	typedef BSTreeNode<K> node;
public:

	bool insert(const K& key)
	{
		return _insert(_root, key);
		/*node* prev = _root;
		node* now = _root;
		if (_root == nullptr)
		{
			_root = new node(key);
			return true;
		}

		while (now)
		{
			if (key > now->key)
			{
				prev = now;
				now = now->right;
			}
			else if (key < now->key)
			{
				prev = now;
				now = now->left;
			}
			else
			{
				return false;
			}
		}

		now = new node(key);
		if (key > prev->key)
		{
			prev->right = now;
		}
		else
		{
			prev->left = now;
		}
		return true;*/

	}

	void InOrder()
	{
		_InOrder(_root);
	}

	bool find(const K& key)
	{
		return _find(_root, key);
		/*node* now = _root;

		while (now)
		{
			if (now->key > key)
			{
				now = now->left;
			}
			else if (now->key < key)
			{
				now = now->right;
			}
			else
			{
				return true;
			}
		}
		return false;*/

	}

	bool Erase(const K& key)
	{

		return _Erase(_root, key);
		/*node* now = _root;
		node* prev = nullptr;

		while (now)
		{
			if (now->key > key)
			{
				prev = now;
				now = now->left;
			}
			else if (now->key < key)
			{
				prev = now;
				now = now->right;
			}
			else
			{
				if (now->left == nullptr)
				{
					if (now == _root)
					{
						_root = now->right;
					}
					else
					{
						if (prev->left == now)
						{
							prev->left = now->right;
						}
						else if(prev->right == now)
						{
							prev->right = now->right;
						}
					}
					delete now;
				}
				else if (now->right == nullptr)
				{
					if (now == _root)
					{
						_root = now->left;
					}
					else
					{
						if (prev->left == now)
						{
							prev->left = now->left;
						}
						else if (prev->right == now)
						{
							prev->right = now->left;
						}
					}
					delete now;
				}
				else
				{
					node* subleft = now->right;
					node* parent = now;
					while (subleft->left)
					{
						parent = subleft;
						subleft = subleft->left;
					}

					swap(subleft->key, now->key);

					if (subleft == parent->left)
					{
						parent->left = subleft->right;
					}
					else if(subleft == parent->right)
					{
						parent->right = subleft->right;
					}

					delete subleft;
				}
				return true;
			}
		}
		return false;*/

	}

	~BSTree()
	{
		Destory(_root);
	}

	BSTree(){}

	BSTree(BSTree<K>& k)
	{
		_root = copy(k._root);
	}

	BSTree<K>& operator=(BSTree<K> k)
	{
		swap(_root = k._root);
		return *this;
	}

private:
	node* _root = nullptr;

	node* copy(node* root)
	{
		if (root == nullptr)
			return nullptr;

		node* newnode = new node(root->key);
		newnode->left = copy(root->left);
		newnode->right = copy(root->right);
		return newnode;
	}

	void Destory(node* root)
	{
		if (root == nullptr)
			return;

		Destory(root->left);
		Destory(root->right);
		delete root;
	}

	void _InOrder(node* root)
	{
		if (root == nullptr)
		{
			return;
		}

		_InOrder(root->left);
		cout << root->key << " ";
		_InOrder(root->right);
	}

	bool _Erase(node*& root, const K& key)
	{
		if (root != nullptr)
		{
			if (root->key > key)
			{
				_Erase(root->left, key);
			}
			else if (root->key < key)
			{
				_Erase(root->right, key);
			}
			else
			{
				node* now = root;
				if (root->left == nullptr)
				{
					root = root->right;
					delete now;
					now = nullptr;
					return true;
				}
				else if (root->right == nullptr)
				{
					root = root->left;
					delete now;
					now = nullptr;
					return true;
				}
				else
				{
					node* subleft = root->right;
					node* parent = root;
					while (subleft->left)
					{
						parent = subleft;
						subleft = subleft->left;
					}

					swap(subleft->key, root->key);
					_Erase(root->right, key);
					return true;
				}

			}
		}
		else
		{
			return false;
		}

	}


	bool _insert(node*& root, const K& key)
	{
		if (root != nullptr)
		{
			if (root->key > key)
			{
				_insert(root->left, key);
			}
			else if (root->key < key)
			{
				_insert(root->right, key);
			}
			else
			{
				return false;
			}
		}
		else
		{
			root = new node(key);
			return true;
		}

	}

	bool _find(node* root, const K& key)
	{
		if (root != nullptr)
		{
			if (root->key > key)
			{
				_find(root->left, key);
			}
			else if (root->key < key)
			{
				_find(root->right, key);
			}
			else
			{
				return true;
			}

		}
		else
		{
			return false;
		}

	}
};